package com.bootdo.market.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.SXSSFWorkBookUtil;
import com.bootdo.market.dao.DuplicateDao;
import com.bootdo.market.domain.ChannelProfitDO;
import com.bootdo.market.service.AacommonService;
import com.bootdo.market.service.ChannelProfitService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author jinse
 * @email market@jinse.com
 * @date 2019-03-31 15:46:44
 */

@Controller
@RequestMapping("/market/channelProfit")
public class ChannelProfitController {
    @Autowired
    private ChannelProfitService channelProfitService;
    @Autowired
    private AacommonService aacommonService;
    @Autowired
    private DuplicateDao duplicateDao;


    @GetMapping()
    @RequiresPermissions("market:channelProfit:channelProfit")
    String ChannelProfit() {
        return "market/channelProfit/channelProfit";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("market:channelProfit:channelProfit")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ChannelProfitDO> channelProfitList = channelProfitService.list(query);
        int total = channelProfitService.count(query);
        PageUtils pageUtils = new PageUtils(channelProfitList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("market:channelProfit:add")
    String add() {
        return "market/channelProfit/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("market:channelProfit:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        ChannelProfitDO channelProfit = channelProfitService.get(id);
        model.addAttribute("channelProfit", channelProfit);
        return "market/channelProfit/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("market:channelProfit:add")
    public R save(ChannelProfitDO channelProfit) {
        BigDecimal result_num_new = new BigDecimal(String.format("%.2f", Double.valueOf(String.valueOf(channelProfit.getChannelprofit())) / Double.valueOf(channelProfit.getChanneldau()) * 100000));
        channelProfit.setChannelarpu(result_num_new);
        if (channelProfitService.save(channelProfit) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("market:channelProfit:edit")
    public R update(ChannelProfitDO channelProfit) {
        BigDecimal result_num_new = new BigDecimal(String.format("%.2f", Double.valueOf(String.valueOf(channelProfit.getChannelprofit())) / Double.valueOf(channelProfit.getChanneldau()) * 100000));
        channelProfit.setChannelarpu(result_num_new);
        channelProfitService.update(channelProfit);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("market:channelProfit:remove")
    public R remove(Integer id) {
        if (channelProfitService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("market:channelProfit:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        channelProfitService.batchRemove(ids);
        return R.ok();
    }

    /**
     * 发布
     */
    @PostMapping("/batchPublish")
    @ResponseBody
    @RequiresPermissions("market:channelProfit:batchPublish")
    public R batchPublish(@RequestParam("ids[]") Integer[] ids) {
        Arrays.stream(ids).forEach(integer -> {
            ChannelProfitDO channelProfit = new ChannelProfitDO();
            channelProfit.setId(integer);
            channelProfit.setPublishstate(1);
            channelProfitService.update(channelProfit);
        });
        return R.ok();
    }

    @RequestMapping(value = "/exportExcel")
    @RequiresPermissions("market:channelProfit:exportExcel")
    public void exportExcel
            (Integer publishstate,
             String beginTime,
             String endTime,
             HttpServletRequest request,
             HttpServletResponse response) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("publishstate", publishstate);
        if (StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)) {
            params.put("beginTime", beginTime);
            params.put("endTime", endTime);
        }
        List<ChannelProfitDO> list = aacommonService.exportChannelData(params);
        List<Object[]> objects = new ArrayList<>();
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Object[] obj = new Object[7];
                ChannelProfitDO taDTO = list.get(i);
                obj[0] = taDTO.getChannelname();
                obj[1] = taDTO.getSdkname();
                obj[2] = taDTO.getChanneldau();
                obj[3] = taDTO.getChannelprofit();
                obj[4] = taDTO.getChannelarpu();
                if (taDTO.getPublishstate() == 1) {
                    obj[5] = "已发布";
                } else {
                    obj[5] = "未发布";
                }
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                obj[6] = formatter.format(taDTO.getCreatetime());
                objects.add(obj);
            }
        }
        Map<Object, List<Object[]>> dataMap = new LinkedHashMap<Object, List<Object[]>>();
        dataMap.put(new Object(), objects);
        String title = "渠道数据统计";
        String[] headers = {"渠道名称", "sdk名称", "活跃用户数", "收益", "arpu/100000", "发布状态", "创建日期"};
        SXSSFWorkBookUtil.exportSheetsForOneObj(title, headers, dataMap,
                request, response);
    }

    @RequestMapping(value = "/channelProfitJudgingUniqueAdd", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R channelProfitJudgingUniqueAdd(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.channelProfitJudgingUniqueAdd(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping(value = "/channelProfitJudgingUniqueEdit", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R channelProfitJudgingUniqueEdit(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.channelProfitJudgingUniqueEdit(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}
